<?php
/**
* 微活动数据模板
* 
* @package app
* @version    1.0
* @author     Ray 33705910@qq.com
* @license    MIT License
* @copyright  2013 - 2015 Ray
* @link       http://wangxiaolei.cn
*
* @package  app
* @extends  \Orm\Model
*/
class Model_Marketing extends \Orm\Model {

	protected static $_table_name = 'marketing';

	protected static $_primary_key = array('id');

	protected static $_observers = array(
		'Orm\Observer_CreatedAt' => array(
			'events' => array('before_insert'),
			'mysql_timestamp' => false,
		),
		'Orm\Observer_UpdatedAt' => array(
			'events' => array('before_save'),
			'mysql_timestamp' => false,
		)
	); 

	/**
	 * @var array	has_many relationships
	 */
	protected static $_has_many = array(
		'prizes' => array(
			'model_to' => '\Model_MarketingPrize',
			'key_from' => 'id',
			'key_to'   => 'marketing_id',
		),
		'records' => array(
			'model_to' => '\Model_MarketingRecord',
			'key_from' => 'id',
			'key_to'   => 'marketing_id',
		),
		'votes' => array(
			'model_to' => '\Model_MarketingVote',
			'key_from' => 'id',
			'key_to'   => 'marketing_id',
		),
		'metadata' => array(
			'model_to' => '\Model_MarketingMetadata',
			'key_from' => 'id',
			'key_to'   => 'parent_id',
		)
	);

	protected static $_belongs_to = array(
		'account' => array(
	        'key_from' => 'account_id',
	        'model_to' => 'Model_WXAccount',
	        'key_to' => 'id',
	        'cascade_save' => true,
	        'cascade_delete' => false,
	    )
	);

	protected static $_eav = array(
        'metadata' => array(
            'attribute' => 'key',
            'value' => 'value',
        ),
    );

	public static $_maps = array(
		'type' => array(
			'SCRATCH_CARDS' => '刮刮卡',
			'SCRATCH_CARDS_V2' => '新版刮刮卡',
			'TURNTABLE' => '大转盘',
			'GAME' => '小游戏',
			'LUCKY' => '幸运机',
			'KEEPWIN' => '一站到底',
			'TASK_CARD' => '任务卡',
			'TESTING' => '趣味小测试',
			'VOTE' => '投票'
		)
	);

	/**
	* 创建微活动信息
	* [example]
	* [/example]
	**/
	public static function do_create($data){

		$entity = Model_Marketing::forge($data);
		if(!empty($data['questions'])){
			$entity->metadata = \Model_MarketingMetadata::forge(array('key' => 'questions','value' => $data['questions'],'user_id' => \Auth::get_user()->id));
		}

		if(isset($data['prizes'])){
			$entity->prizes = array();
			foreach ($data['prizes'] as $value) {
				array_push($entity->prizes, \Model_MarketingPrize::forge($value));				
			}
		}
		if($entity->save()){
			return array('ret' => 'succ', 'msg' => '保存成功', 'data' => $entity);
		}else{
			return array('ret' => 'err', 'msg' => '保存失败');
		}
	}

	/**
	* 删除微活动信息
	* @param $id int 收入标识
	* [example]
	* $result = Model_Marketing::do_delete(1);
	* var_dump($result);
	* [/example]
	*/
	public static function do_delete($id){		
		$entity = Model_Marketing::find($id);
		if(! $entity){
			return array('ret' => 'err', 'msg' => '微活动不存在，无法删除');
		}
		if($entity->delete()){
			return array('ret' => 'succ', 'msg' => '删除成功');
		}else{
			return array('ret' => 'err', 'msg' => '删除失败');
		}
	}

	/**
	* 更新微活动信息
	* [example]
	* [/example]
	**/
	public static function do_update($id, $data){
		$entity = Model_Marketing::find($id);
		if(! $entity){
			return array('ret' => 'err', 'msg' => '微活动不存在，无法编辑');
		}
		$entity->set($data);
		if($entity->save()){
			return array('ret' => 'succ', 'msg' => '保存成功');
		}else{
			return array('ret' => 'err', 'msg' => '保存失败');
		}
	}

	/**
	* 根据收入ID获取单条数据
	* @param $id int 收入ID
	* @param $is_array int 是否转换为Array
	*/
	public static function getData($id, $is_array = 0){
		$entity = Model_Marketing::find($id);

		if($entity){
			return $is_array ? $entity->to_array() : $entity;
		}
		return;
	}

	/**
	* 根据条件获取单条数据
	* @param $params int 查询条件
	* @param $is_array int 是否转换为Array
	*/
	public static function getItem($params = array(), $is_array = 0){
		$entity = Model_Marketing::query()
				  ->related('records');
		if($params && is_array($params)){
			foreach ($params as $key => $value) {
				if(is_array($value)){
					$entity->where($value[0], $value[1], $value[2]);
				}else{
					$entity->where($key, $value);
				}				
			}
		}
		$entity = $entity->get_one();
		return $is_array == 1 ? $entity->to_array() : $entity;
	}

	/**
	* 根据查询条件、排序条件获取数据
	* @param $fields String 显示字段列表
	* @param $params Array 查询条件
	* @param $tables Array 多表查询
	* @param $order_by Array 排序字段(array('字段名' => 'ASC|DESC'))
	* @param $page int 分页状态(0.不分页 1.分页)
	*/
	public static function getItems($fields = '*', $params = array(), $tables = array(), $order_by = array(), $page = 0){
		$items = Model_Marketing::query();
		//判断是否多表查询
		if($tables){
			$items->related($tables);
		}
		//判断是否有查询条件
		if($params){
			foreach ($params as $key => $value) {
				if(is_array($value)){
					$items->where($key, $value[0], $value[1]);
				}else{
					$items->where($key, $value);
				}
				
			}
		}
		//判断是否有排序条件
		if($order_by){
			foreach ($order_by as $key => $value) {
				if(is_numeric($key)){
					$items->order_by($value);
				}else{
					$items->order_by($key, $value);
				}				
			}
		}
		//$items->limit(100);
		//判断是否分页
		if($page){
			return $items;
		}else{
			return $items->get();
		}
	}
}
?>